package org.lzy.shop.address

import android.text.TextUtils
import org.lzy.shop.Constant
import org.lzy.shop.R
import org.lzy.shop.base.BaseMvvmActivity
import org.lzy.shop.databinding.ActivityAddAddressBinding
import org.lzy.shop.datas.AddAddressRequest
import org.lzy.shop.datas.EditAddressRequest
import org.lzy.shop.response.AddressResponse
import org.lzy.shop.view.CityPickerDialog
import org.lzy.shop.viewmodel.AddressModel

/**
 * 添加/编辑地址
 */
class AddAddressActivity  : BaseMvvmActivity<ActivityAddAddressBinding, AddressModel>({ ActivityAddAddressBinding.inflate(it) })  {
     var mProvince: String? = null
     var mDistrict: String? = null
     var mCity: String? = null
     var mIsDefault = false
     var addressId = 0
    var addressResponse: AddressResponse? = null
    //1:新增 , 2:编辑
    private var addressType = -1



    override fun initView() {
        if (intent.extras != null) {
            val bundle = intent.extras
            addressType = bundle!!.getInt(Constant.KEY_EXTRAS_TYPE)
            //编辑时候直接把整个数据源搬过来
            addressResponse = bundle.getSerializable("addressData", AddressResponse::class.java)
            addressId = bundle.getInt("addressId")
        }
        if (addressType == 1) { //新增收货地址
            setTitleText(getResString(R.string.add_address))
        } else if (addressType == 2) {
            setTitleText(getResString(R.string.edit_address))
            addressResponse?.let {
                binding.actAddAddressEtContactName.setText(addressResponse!!.contact+addressResponse!!.phone)
                binding.actAddAddressEtContactPhone.setText(addressResponse!!.phone)
                binding.actAddAddressEtDetailAddress.setText(addressResponse!!.address)
                binding.actAddAddressTvLocationCity.text = addressResponse!!.province+addressResponse!!.district+addressResponse!!.city
                mProvince=addressResponse!!.province
                mCity=addressResponse!!.city
                mDistrict=addressResponse!!.district
            }
        }
        mIsDefault = true
        binding.defaultaddressCb.setOnCheckedChangeListener { p0, b ->
            if (b) {
                mIsDefault=true
            }else{
                mIsDefault=false
            }
        }

    }

    override fun initData() {
        //添加/编辑提交回调
        mViewModel.addressOperationData.observeWithLifecycle(this){string ->
            dismissLoading()
            showSuccessToast(string)
            finish()


        }
    }

    override fun requestData() {
    }

    override fun allClick() {
        //保存
        binding.actAddAddressBtnSaveAddress.setOnClickListener {
            saveAddress()
        }
        //选择城市
        binding.actAddAddressLlChooseAddress.setOnClickListener {
            selectAddress()
        }
    }

    //选择城市地址
    fun selectAddress() {
        // 使用自定义的城市选择对话框
        val dialog = CityPickerDialog(this) { province, city, district ->
            // 更新选中的地址信息
            mProvince = province
            mCity = city
            mDistrict = district
            // 更新UI显示
            binding.actAddAddressTvLocationCity.text = "$province$city$district"
        }
        dialog.show()

    }

    fun saveAddress() {
        val showname = binding.actAddAddressEtContactName.text.toString()
        val mobile = binding.actAddAddressEtContactPhone.text.toString()
        val address = binding.actAddAddressEtDetailAddress.text.toString()
        if (TextUtils.isEmpty(showname)) {
            showToast(getResString(R.string.empty_addname))
            return
        }
        if (TextUtils.isEmpty(mobile)) {
            showToast(getResString(R.string.mobile_empty))
            return
        }
        if (mobile.length < 11) {
            showToast(getResString(R.string.mobile_error))
            return
        }


        if (TextUtils.isEmpty(address)) {
            showToast(getResString(R.string.empty_address))
            return
        }
        //新建地址id不带
        if (addressType == 1) {
            val request = AddAddressRequest()
            request.phone=binding.actAddAddressEtContactPhone.text.toString()
            request.contact=binding.actAddAddressEtContactName.text.toString()
            request.address=binding.actAddAddressEtDetailAddress.text.toString()
            request.province=mProvince
            request.city=mCity
            request.district=mDistrict
            request.isDefault=mIsDefault
            mViewModel.addAddress(request)
        } else {
            //编辑地址id要带
            val request = EditAddressRequest()
            request.id=addressId
            request.phone=binding.actAddAddressEtContactPhone.text.toString()
            request.contact=binding.actAddAddressEtContactName.text.toString()
            request.address=binding.actAddAddressEtDetailAddress.text.toString()
            request.province=mProvince
            request.city=mCity
            request.district=mDistrict
            request.isDefault=mIsDefault
            mViewModel.updateAddress(request)
        }
    }
}